# -*- coding: utf-8 -*-
import pymysql

class DB():
    def __init__(self, HOST, PROT, USER, PWD , DBNAME):
        self.HOST = HOST
        self.PROT = PROT
        self.USER = USER
        self.PWD = PWD
        self.DBNAME = DBNAME
        self.connect = self.getConnect()

    def getConnect(self):
        return pymysql.connect(
            host=self.HOST,
            port=self.PROT,
            user=self.USER,
            passwd=self.PWD,
            db=self.DBNAME,
            charset="utf8"
        )

    def query(self, sqlStr):
        db = self.connect
        cursors = db.cursor()
        try:
            cursors.execute(sqlStr)
            db.commit()
            data = cursors.fetchall()
        except:
            db.rollback()
            return False

        return data

    def insert(self, sqlStr, param):
        db = self.connect
        cursors = db.cursor()
        try:
            cursors.execute(sqlStr, param)
            db.commit()
            data = cursors.fetchall()
        except:
            db.rollback()
            return False

        return data

def connect():
    connect = DB('localhost', 3306, 'root', '123456', 'loldytt')
    return connect

if __name__ == "__main__":
        print connect().query("insert into blog (title, ctime) values('今天是个好日子','2012-12-01 13:22:10')")
